package it.iubar.tkdscore;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import net.java.games.input.Controller;
import net.java.games.input.Controller.Type;
import net.java.games.input.ControllerEnvironment;

/**
 *
 * @author borgo
 */
public class TkdGlobal {

    public static int first_gamepad_index = -1;

    private static String getSeparator() {
        String sep = System.getProperty("file.separator");
        String str = "File Separator: " + sep;
        System.out.println(str);
        return sep;
    }
    static final String filename = "taewkondo_score.config";

    static File getConfigFile() {
        String path = TkdGlobal.getDocsFolder();
        String fullpath = path + TkdGlobal.getSeparator() + TkdGlobal.filename;
        File f = new File(fullpath);
        System.out.println("config file: " + fullpath);
        return f;
    }

    private static String getDocsFolder() {
        String path = System.getProperty("user.home");
        String str = "User Home Path: " + path;
        System.out.println(str);
        return path;
    }

    public static void showInfo(String message) {
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(it.iubar.tkdscore.TkdScoreApp.class).getContext().getResourceMap(TkdScoreView.class);
        JOptionPane.showMessageDialog(null, message,
                "Info", JOptionPane.INFORMATION_MESSAGE, resourceMap.getIcon("info.icon"));
    }

    static List _getControllerList(JFrame f) {
        List list = new ArrayList();
        boolean bcontinue = false;
        Controller[] ca = null;
        try {
            ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
        } catch(Exception e){
            e.printStackTrace();
            String msg = "Jinput library not found." + "\r\n" + "Please, install it before running the application.";
                JOptionPane.showMessageDialog(f,
                        msg,
                        "Warning message",
                        JOptionPane.WARNING_MESSAGE);        
        }
        if(ca!=null){
        for (int i = 0; i < ca.length; i++) {
            bcontinue = false;
            System.out.println("Index: " + i);
            System.out.println("Found input device. Name is: " + ca[i].getName());
            System.out.println("Found input device. Class is: " + ca[i].getClass().getName());

            Controller controller = ca[i];
            List list2 = new ArrayList();
            if (controller instanceof net.java.games.input.Keyboard) {
                bcontinue = true;
            } else if (controller instanceof net.java.games.input.Controller) {
                bcontinue = true;
            }

            if (bcontinue) {
                String name = controller.getName();
                Type type = controller.getType();
                list2.add(name);
                list2.add(type);
                list2.add(i);
                list.add(list2);
            }

        }
        }
        return list;
    }

    static int _getNumGamepads() {
        int n = 0;
        List list = _getGamepadsList();
        if (list != null) {
            n = list.size();
        }
        return n;
    }

    static List _getGamepadsList() {
        first_gamepad_index = -1;
        List list = new ArrayList();
        boolean bcontinue = false;
        Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
        for (int i = 0; i < ca.length; i++) {
            bcontinue = false;
            System.out.println("Found input device.");
            System.out.println("Index: " + i);
            System.out.println("Name is: " + ca[i].getName());
            System.out.println("Class is: " + ca[i].getClass().getName());
            System.out.println("Port number is: " + ca[i].getPortNumber());

            Controller controller = ca[i];
            List list2 = new ArrayList();
            if (controller instanceof net.java.games.input.Keyboard) {
                bcontinue = true;
            } else if (controller instanceof net.java.games.input.Controller) {
                bcontinue = true;
            }

            if (bcontinue) {
                String name = controller.getName();
                int port_number = controller.getPortNumber();
                Type type = controller.getType();
                if (type != null) {
                    String typename = type.toString();
                    if (typename.equals("Gamepad")) {
                        list2.add(name);
                        list2.add(type);
                        list2.add(i);
                        list2.add(port_number);
                        if (first_gamepad_index < 0) {
                            first_gamepad_index = i;
                        }
                        list.add(list2);
                    }
                }
            }

        }
        return list;
    }

    public static void print(String string) {
        System.out.println(string);
    }

    static int toInt(String str) {
        int n = 0;
        if ((str != null) && (!str.equals(""))) {
            Integer a = Integer.valueOf(str);
            if (a != null) {
                n = a.intValue();
            }
        }
        return n;
    }

        static boolean openWebPage(String url) {
            return openWebPage(url, null);
        }

    static boolean openWebPage(String url, JFrame frame) {

    //minimizes the app
    if (frame != null){
        frame.setExtendedState(JFrame.ICONIFIED);
    }

    String os = System.getProperty("os.name").toLowerCase();
    Runtime rt = Runtime.getRuntime();
    try
    {
            if (os.indexOf( "win" ) >= 0)
            {
              String[] cmd = new String[4];
              cmd[0] = "cmd.exe";
              cmd[1] = "/C";
              cmd[2] = "start";
              cmd[3] = url;
              rt.exec(cmd);
            }
            else if (os.indexOf( "mac" ) >= 0)
            {
                rt.exec( "open " + url);
            }
            else
            {
              //prioritized 'guess' of users' preference
              String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror",
                  "netscape","opera","links","lynx"};

              StringBuffer cmd = new StringBuffer();
              for (int i=0; i<browsers.length; i++)
                cmd.append( (i==0  ? "" : " || " ) + browsers[i] +" \"" + url + "\" ");

              rt.exec(new String[] { "sh", "-c", cmd.toString() });
              //rt.exec("firefox http://www.google.com");
              //System.out.println(cmd.toString());

           }
    }catch (IOException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(frame,
                                            "\n\n The system failed to invoke your default web browser while attempting to access: \n\n " + url + "\n\n",
                                            "Browser Error",
                                            JOptionPane.WARNING_MESSAGE);

        return false;
    }
    return true;
}


}
